package org.onionframework.faces.action;

import java.util.List;

import javax.annotation.PostConstruct;

import org.onionframework.faces.extension.ViewAction;
import org.onionframework.faces.model.Food;
import org.onionframework.faces.service.FoodService;
import org.onionframework.faces.web.action.OnionAction;
import org.onionframework.persistence.query.OnionQuery;
import org.springframework.beans.factory.annotation.Autowired;

@ViewAction("faces.food")
public class FoodAction extends OnionAction<Food>{

	private static final long serialVersionUID = -65148933272205471L;

	@Autowired
	private FoodService foodService;
	private List<Food> foods;
	private Food one;
	
	@Override
	@PostConstruct
	public void init(){
		super.setService(this.foodService);
		//this.initFirstData();
		this.foods = this.getService().findAll();
		super.init();
		this.entity = new Food();
		this.entity.setName("abc");
	}
	
	/**
	 * 
	 */
	private void initFirstData() {
		Food food = new Food();
		food.setName("abc");
		this.getService().save(food);
		
		food = new Food();
		food.setName("def");
		this.getService().save(food);
	}

	@Override
	protected void initDatas(OnionQuery onionQuery) {
		this.datas = this.findPage(onionQuery);
	}
	
	@Override
	public void onEdit(){
		this.entity.getId();
		
		//FacesMessages.addErrorMessage("出错啦~");
		
		//FacesMessages.addMessage("hello哈哈哈");
		//FacesMessages.addMessage("hello呵呵呵");
	}
	
	@Override
	public void save(){
		/*if(1==1)
			throw Exceptions.business("测试异常", null);*/
		super.save();
	}

	public List<Food> getFoods() {
		return this.foods;
	}
	
	public void printOne(){
		this.entity.setName("def");
		System.out.println(this.one);
	}

	public Food getOne() {
		return this.one;
	}

	public void setOne(Food one) {
		this.one = one;
	}
	
	
}
